package com.hkts.beltweigher.master.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hkts.beltweigher.master.entity.InstrumentControl;
import com.hkts.beltweigher.master.entity.YbClassesAccumulate;
import com.hkts.beltweigher.master.mapper.InstrumentControlMapper;
import com.hkts.beltweigher.master.service.IInstrumentControlService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.beltweigher.utils.Result;
import com.hkts.beltweigher.utils.ResultUtil;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hml
 * @since 2024-04-19
 */
@Service
@Transactional
public class InstrumentControlServiceImpl extends ServiceImpl<InstrumentControlMapper, InstrumentControl> implements IInstrumentControlService {


    @Autowired
    private InstrumentControlMapper instrumentControlMapper;

    @Override
    public IPage<InstrumentControl> selectPage(Page<InstrumentControl> page, InstrumentControl instrumentControl) {
        QueryWrapper<InstrumentControl> query = Wrappers.query();
        return instrumentControlMapper.selectPage(page, query);
    }


    @Override
    public List<InstrumentControl> selectList(InstrumentControl instrumentControl) {
        QueryWrapper<InstrumentControl> query = Wrappers.query();
        return instrumentControlMapper.selectList(query);
    }

    @Override
    public Result<Object> updateStatus(InstrumentControl instrumentControl) {
        Integer id = instrumentControl.getId();
        //没有传id 默认
        if (id == null) {
            QueryWrapper<InstrumentControl> query = Wrappers.query();
            List<InstrumentControl> instrumentControls = instrumentControlMapper.selectList(query);
            List<InstrumentControl> collect = instrumentControls.stream().filter(instrument ->
                    !instrument.getId().equals(3)).collect(Collectors.toList());
            for (int i = 0; i < collect.size(); i++) {
                collect.get(i).setInstrumentStatus(instrumentControl.getInstrumentStatus());
            }
            this.updateBatchById(collect);
        } else {
            QueryWrapper<InstrumentControl> query = Wrappers.query();
            query.lambda().eq(InstrumentControl::getId, instrumentControl.getId());
            InstrumentControl instrumentControl1 = instrumentControlMapper.selectOne(query);
            instrumentControl1.setInstrumentStatus(instrumentControl.getInstrumentStatus());
            this.updateById(instrumentControl1);
        }
        if(instrumentControl.getInstrumentStatus().equals("1")){
           return ResultUtil.success("启动成功！");
        }else{
            return ResultUtil.success("停止成功！");
        }
    }

    @Override
    public Map<Integer, String> getInstrumentStatus() {
        QueryWrapper<InstrumentControl> query = Wrappers.query();
        List<InstrumentControl> instrumentControls = instrumentControlMapper.selectList(query);
        Map<Integer, String> collect = instrumentControls.stream().collect(Collectors.toMap(InstrumentControl::getId, InstrumentControl::getInstrumentStatus));
        return collect;
    }
}
